import { createClock, createNullEntity } from '$lib';
import { fromTemplate } from '$lib/builder';
import { addComponent, createWorld, type Entity, type IWorld } from '$lib/ecs';
import { randomSeed } from '$lib/random';
import { newIntroMission } from './intro';
import { setMission } from './mission';
import { playerTemplate } from './player';
import { enumStats } from './stat';

export type Game = {
  world: IWorld,
  pc: Entity,
};

export const addPc = (world) => {
  const pc = fromTemplate(world, playerTemplate);
  for (let [name, stat] of enumStats()) {
    addComponent(world, stat, pc);
    stat.amount[pc] = 8;
  }
  return pc;
};

export const newGame = () => {
  randomSeed(123);

  const world = createWorld();

  createNullEntity(world);
  createClock(world);

  const pc = addPc(world);

  setMission(world, pc, newIntroMission(world));

  return {
    world,
    pc,
  };
};
